package com.swpu.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swpu.project.common.BaseResponse;
import com.swpu.project.mapper.RadiationMapper;
import com.swpu.project.model.entity.LoginUser;
import com.swpu.project.model.entity.Radiation;
import com.swpu.project.service.IRadiationService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author swpuyanxiegroup1
 * @since 2024-07-29
 */
@Service
public class RadiationServiceImpl extends ServiceImpl<RadiationMapper, Radiation> implements IRadiationService {

    @Override
    public BaseResponse queryData(String belongId) {
        List<Radiation> radiations = lambdaQuery().eq(Radiation::getRadiationBelong, belongId).
                select(
                        Radiation::getRadiationDataId,
                        Radiation::getRadiationData,
                        Radiation::getRadiationName).
                list();


        return BaseResponse.ok(radiations);
    }

    @Override
    public BaseResponse updateMineral(List<Radiation> minerals, String blockNum) {
        if(!checkAuthority(blockNum))
            return BaseResponse.fail("您无权进行该操作或数据填写有误");




//        return BaseResponse.iFSuccess(updateBatchById(minerals));
        return BaseResponse.iFSuccess(saveOrUpdateBatch(minerals));

    }



    private boolean checkAuthority(String blockNum) {
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<String> blockNums = principal.getBlockNums();
        if(!blockNums.contains(blockNum))
            return  false;
        return true;

    }


}
